package control;

import utils.Var;
import utils.SettingInfo;
import utils.Setting;
import utils.DBConnection;

import java.sql.Connection;

import interceptor.InjectConnection;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import account.AccountUtils;

/**
 * Created by IntelliJ IDEA.
 * User: Shawn
 * Date: 2009-6-6
 * Time: 11:57:24
 */
public class ControlAction {

  private Connection con;

  @InjectConnection(autoCommit = DBConnection.AutoCommiteOff)
  public void setCon(Connection con) {
    this.con = con;
  }

  private ControlSettingInfo settingInfo;

  public ControlSettingInfo getSettingInfo() {
    return settingInfo;
  }

  public void setSettingInfo(ControlSettingInfo settingInfo) {
    this.settingInfo = settingInfo;
  }

  public String getControlSetting() throws Exception {
    HttpSession session = ServletActionContext.getRequest().getSession();
    if (!AccountUtils.hasRight(session, Var.RIGHT_CONTROL_IP)) throw new Exception("deny!");

    SettingInfo computerSum = Setting.getSetting(con, ControlVar.SETTING_COMPUTER_SUM);
    SettingInfo ipStart = Setting.getSetting(con, ControlVar.SETTING_IP_START);
    SettingInfo ipEnd = Setting.getSetting(con, ControlVar.SETTING_IP_END);

    settingInfo = new ControlSettingInfo();
    settingInfo.setComputerSum(computerSum == null ? 0 : Integer.parseInt(computerSum.getSvalue()));
    settingInfo.setIpStart(ipStart == null ? null : ipStart.getSvalue());
    settingInfo.setIpEnd(ipEnd == null ? null : ipEnd.getSvalue());

    return Var.ACTION_SUCCESS;
  }

  public String setControlSetting() throws Exception {
    HttpSession session = ServletActionContext.getRequest().getSession();
    if (!AccountUtils.hasRight(session, Var.RIGHT_CONTROL_IP)) throw new Exception("deny!");

    Setting.delSetting(con, ControlVar.SETTING_COMPUTER_SUM);
    Setting.delSetting(con, ControlVar.SETTING_IP_START);
    Setting.delSetting(con, ControlVar.SETTING_IP_END);

    SettingInfo computerSum = new SettingInfo(ControlVar.SETTING_COMPUTER_SUM, String.valueOf(settingInfo.getComputerSum()));
    SettingInfo ipStart = new SettingInfo(ControlVar.SETTING_IP_START, settingInfo.getIpStart());
    SettingInfo ipEnd = new SettingInfo(ControlVar.SETTING_IP_END, settingInfo.getIpEnd());

    Computer.delComputer(con);
    if (settingInfo.getType() == ControlVar.SETTING_TYPE_SUM) {
      Setting.addSetting(con, computerSum);

      for (int i = 0; i < settingInfo.getComputerSum(); i++) {
        ComputerInfo info = new ComputerInfo();
        Computer.addComputer(con, info);
      }
    }
    else {
      Setting.addSetting(con, ipStart);
      Setting.addSetting(con, ipEnd);

      String start = settingInfo.getIpStart();
      String end = settingInfo.getIpEnd();

      int s = Integer.parseInt(start.substring(start.lastIndexOf(".") + 1));
      int e = Integer.parseInt(end.substring(end.lastIndexOf(".") + 1));
      if (e < s) throw new Exception("input error");

      String sbase = start.substring(0, start.lastIndexOf(".") + 1);
      String ebase = end.substring(0, end.lastIndexOf(".") + 1);
      if (!sbase.equals(ebase)) throw new Exception("input error");

      for (; s <= e; s++) {
        ComputerInfo info = new ComputerInfo();
        info.setIp(sbase + s);
        Computer.addComputer(con, info);
      }
    }

    return Var.ACTION_SUCCESS;
  }
}
